{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f70a2b3b",
   "metadata": {},
   "source": [
    "-----------\n",
    "\n",
    "* 时间：2022-10-24  /  week10\n",
    "* 课程：Python语言\n",
    "* 学习记录人：陈泽鑫\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbdda0ec",
   "metadata": {},
   "source": [
    "# 本周学习目标\n",
    "> 1. while循环\n",
    "> 2. 购物车实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd92b16a",
   "metadata": {},
   "source": [
    "## 1.while循环\n",
    "\n",
    "* 回顾: for 循环: for + range()可以指定循环的次数--->关于网页URL队列的抓取实践\n",
    "* while循环：当某种条件成立时，停止循环\n",
    "* while True ：无线循环/死循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b8143587",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙，缠缠绵绵到天涯~~ 0\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 1\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 2\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 3\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 4\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 5\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 6\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 7\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 8\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 9\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 10\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 11\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 12\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 13\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 14\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 15\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 16\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 17\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 18\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 19\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 20\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 21\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 22\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 23\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 24\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 25\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 26\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 27\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 28\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 29\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 30\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 31\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 32\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 33\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 34\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 35\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 36\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 37\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 38\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 39\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 40\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 41\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 42\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 43\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 44\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 45\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 46\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 47\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 48\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 49\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 50\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 51\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 52\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 53\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 54\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 55\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 56\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 57\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 58\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 59\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 60\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 61\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 62\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 63\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 64\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 65\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 66\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 67\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 68\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 69\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 70\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 71\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 72\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 73\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 74\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 75\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 76\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 77\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 78\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 79\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 80\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 81\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 82\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 83\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 84\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 85\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 86\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 87\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 88\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 89\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 90\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 91\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 92\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 93\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 94\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 95\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 96\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 97\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 98\n",
      "你是风儿我是沙，缠缠绵绵到天涯~~ 99\n"
     ]
    }
   ],
   "source": [
    "# 指定次数\n",
    "count = 0\n",
    "while True:\n",
    "    print(\"你是风儿我是沙，缠缠绵绵到天涯~~\",count)\n",
    "    count = count+1\n",
    "    if count == 100:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dcf1e062",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：125615\n",
      "密码：2562\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'user01' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36m<cell line: 6>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m username \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m账号：\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m      7\u001b[0m password \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m密码：\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 8\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43muser01\u001b[49m \u001b[38;5;241m==\u001b[39m username \u001b[38;5;129;01mand\u001b[39;00m password01 \u001b[38;5;241m==\u001b[39m password:\n\u001b[0;32m      9\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m欢迎登陆~欢迎\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m+\u001b[39muser01)\n\u001b[0;32m     10\u001b[0m     \u001b[38;5;28;01mbreak\u001b[39;00m \u001b[38;5;66;03m# 中止循环\u001b[39;00m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'user01' is not defined"
     ]
    }
   ],
   "source": [
    "# 2.while True循环，可以让用户重复输入\n",
    "# user01 = \"admin\"\n",
    "# password01 = \"123456\"\n",
    "\n",
    "while True:\n",
    "    username = input(\"账号：\")\n",
    "    password = input(\"密码：\")\n",
    "    if user01 == username and password01 == password:\n",
    "        print(\"欢迎登陆~欢迎\"+user01)\n",
    "        break # 中止循环\n",
    "    #情景二：账号不存在或密码不正确，用户可以输入三次，三次之后一小时后再来尝试\n",
    "    else:\n",
    "        if count<2:\n",
    "            print(\"账号不存在或密码不正确！请重新输入！您还有\"+str(2-count)+\"次尝试的机会\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确！请一小时后再来尝试\")\n",
    "            break\n",
    "        count = count+1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41572ee2",
   "metadata": {},
   "source": [
    "# 2.购物车项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "feae3682",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75fbb567",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：66666666\n",
      "------商品列表------\n",
      "商品序号: 0 蜜桃四季春 8元\n",
      "商品序号: 1 雪顶咖啡 9元\n",
      "商品序号: 2 柠檬水 6元\n",
      "商品序号: 3 芝士奶盖 10元\n",
      "请输入商品编号：2\n",
      "商品购买成功，正在制作中...你的余额还有66666660元\n",
      "------商品列表------\n",
      "商品序号: 0 蜜桃四季春 8元\n",
      "商品序号: 1 雪顶咖啡 9元\n",
      "商品序号: 2 柠檬水 6元\n",
      "商品序号: 3 芝士奶盖 10元\n",
      "请输入商品编号：3\n",
      "商品购买成功，正在制作中...你的余额还有66666650元\n",
      "------商品列表------\n",
      "商品序号: 0 蜜桃四季春 8元\n",
      "商品序号: 1 雪顶咖啡 9元\n",
      "商品序号: 2 柠檬水 6元\n",
      "商品序号: 3 芝士奶盖 10元\n",
      "请输入商品编号：1\n",
      "商品购买成功，正在制作中...你的余额还有66666641元\n",
      "------商品列表------\n",
      "商品序号: 0 蜜桃四季春 8元\n",
      "商品序号: 1 雪顶咖啡 9元\n",
      "商品序号: 2 柠檬水 6元\n",
      "商品序号: 3 芝士奶盖 10元\n",
      "请输入商品编号：2\n",
      "商品购买成功，正在制作中...你的余额还有66666635元\n",
      "------商品列表------\n",
      "商品序号: 0 蜜桃四季春 8元\n",
      "商品序号: 1 雪顶咖啡 9元\n",
      "商品序号: 2 柠檬水 6元\n",
      "商品序号: 3 芝士奶盖 10元\n",
      "请输入商品编号：3\n",
      "商品购买成功，正在制作中...你的余额还有66666625元\n",
      "------商品列表------\n",
      "商品序号: 0 蜜桃四季春 8元\n",
      "商品序号: 1 雪顶咖啡 9元\n",
      "商品序号: 2 柠檬水 6元\n",
      "商品序号: 3 芝士奶盖 10元\n"
     ]
    }
   ],
   "source": [
    "# 1.运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "product_list = [\n",
    "   [\"蜜桃四季春\",8],\n",
    "    [\"雪顶咖啡\",9],\n",
    "    [\"柠檬水\",6],\n",
    "    [\"芝士奶盖\",10],\n",
    "]\n",
    "shopping_car =[]\n",
    "AliPay = int(input(\"请输入余额：\"))\n",
    "while True:\n",
    "    print(\"------商品列表------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品序号:\",i,product_list[i][0],str(product_list[i][1])+\"元\")\n",
    "    # 2. 让用户输入商品编号进行商品的购买。 ：直接用商品列表的 index 作为商品编号\n",
    "\n",
    "    user_choice = input(\"请输入商品编号：\")\n",
    "    # 情景一：输入商品编号\n",
    "    if user_choice.isdigit():\n",
    "        # 情景一：余额大于商品价格\n",
    "        if AliPay >= product_list[int(user_choice)][1]:\n",
    "            AliPay = AliPay - product_list[int(user_choice)][1]\n",
    "            shopping_car.append(product_list[int(user_choice)])\n",
    "            print(\"商品购买成功，正在制作中...你的余额还有%s元\"%(AliPay))\n",
    "\n",
    "    # 情景二：余额小于商品价格\n",
    "        else:\n",
    "            print(\"您的余额不足，请更换支付方式~~~\")\n",
    "    #情景2：用户输入退出，结束购买\n",
    "    elif user_choice == \"q\":\n",
    "        print(\"您已购买的商品：\",shopping_car)\n",
    "        print(\"您的余额还有%s,欢迎下次光临~~%(AliPay)\")\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f0985a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假设用户选择了0\n",
    "product_list[0][1]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
